﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

 

namespace document_manage
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        MySQLConn con = new MySQLConn();
        //扫描路径
        DirectoryInfo theFolder = new DirectoryInfo(@"F:\Fighting\LogRecord");
        

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Menu w1 = new Menu();
            w1.Show();
            this.Close();
        }
  
        //扫描方法
        private  void scan(FileSystemInfo info)
        {
            if (!info.Exists) return;
            DirectoryInfo dir = info as DirectoryInfo;
            //不是目录
            if (dir == null) return;
            FileSystemInfo[] files = dir.GetFileSystemInfos();
            for(int i =0; i< files.Length;i++)
            {
                FileInfo file = files[i] as FileInfo;
                //是文件
                if (file != null)
                {
                    //插入数据库
                    if (file.Extension == ".docx" || file.Extension == ".txt" || file.Extension == ".xlsx" || file.Extension == ".pdf" || file.Extension == ".pptx")
                    {
                        string sql = "insert into file(file_name,file_path,file_creation_time,file_last_access_time,file_type)" +
                          "values('" + file.Name + "','" + file.Directory.ToString().Replace("\\", "\\\\") + "','" + file.CreationTime + "','" + file.LastAccessTime + "','" + file.Extension + "')";
                        con.ExecuteQuery(sql);
                    }
                }
                else scan(files[i]);
            }
        }//end scan

        private void Scan_Click(object sender, RoutedEventArgs e)
        {
            string sql = "truncate table file ";
            con.ExecuteQuery(sql);
            scan(theFolder);
            MessageBox.Show("扫描完成");
        }
    }
}
